package com.wiretap.action.admin.manage;

import java.io.PrintWriter;
import java.util.Date;

import com.google.gson.Gson;
import com.wiretap.action.WiretapActionSupport;
import com.wiretap.po.User;
import com.wiretap.service.AdminService;
import com.wiretap.service.UserService;
import com.wiretap.util.CommenUtil;
import com.wiretap.util.Constants;
import com.wiretap.util.Pager;

public class UserManageAction extends WiretapActionSupport {

	private static final long serialVersionUID = 1161924886766044042L;
	private static final String CONTENT_TYPE = "text/html; charset=utf-8";
	private Pager<User> users;
	private int cPage;
	private UserService userService;
	private AdminService adminService;
	private String uid;
	private boolean forbidden = false;
	private int startHour;
	private int startMinute;
	private int endHour;
	private int endMinute;
	private String admins;
	private String aid;

	// 用户列表
	@Override
	public String execute() throws Exception {
		cPage = cPage < 1 ? 1 : cPage;
		users = userService.listAllUser(getAdminUsername(), isSuperAdmin(), cPage, Constants.DEFAULT_PAGESIEZE);
		Gson gson = new Gson();
		admins = gson.toJson(adminService.getAllAdministrator());
		return SUCCESS;
	}

	// 删除用户
	public void deleteUser() throws Exception {
		response.setContentType(CONTENT_TYPE);
		PrintWriter out = response.getWriter();
		if (CommenUtil.isEmpty(uid)) {
			out.print("error");
		} else {
			String[] ids = uid.split(",");
			if (!userService.setUserVisibility(ids, false)) {
				// if (!userService.removeUsers(ids)) {
				out.print("error");
			} else {
				out.print("success");
			}
		}
		out.flush();
		out.close();
	}

	@SuppressWarnings("deprecation")
	public void updateForbiddenTime() throws Exception {
		response.setContentType(CONTENT_TYPE);
		PrintWriter out = response.getWriter();
		Date startTime = null;
		Date endTime = null;
		if (forbidden) {
			startTime = new Date();
			startTime.setHours(startHour);
			startTime.setMinutes(startMinute);
			endTime = new Date();
			endTime.setHours(endHour);
			endTime.setMinutes(endMinute);
		}
		if (userService.updateForbiddenTime(uid, forbidden, startTime, endTime)) {
			out.print("success");
		} else {
			out.print("failed");
		}
		out.flush();
		out.close();
	}

	public void updatePhoneForbiddenTime() throws Exception {
		response.setContentType(CONTENT_TYPE);
		PrintWriter out = response.getWriter();
		Date startTime = null;
		Date endTime = null;
		if (forbidden) {
			startTime = new Date();
			startTime.setHours(startHour);
			startTime.setMinutes(startMinute);
			endTime = new Date();
			endTime.setHours(endHour);
			endTime.setMinutes(endMinute);
		}
		if (userService.updatePhoneForbiddenTime(uid, forbidden, startTime, endTime)) {
			out.print("success");
		} else {
			out.print("failed");
		}
		out.flush();
		out.close();
	}
	
	public void getUserCount() throws Exception {
		response.setContentType(CONTENT_TYPE);
		PrintWriter out = response.getWriter();
		long count = userService.getUserCount();
		out.print(count);
		out.flush();
		out.close();
	}

	public void changeUserOwner()throws Exception{
		response.setContentType(CONTENT_TYPE);
		PrintWriter out = response.getWriter();
		if(CommenUtil.isEmpty(aid) ||CommenUtil.isEmpty(uid)){
			out.print("failed");
		}else{
			if(userService.changeUserOwner(uid, aid)){
				out.print("success");
			}else{
				out.print("failed");
			}
		}
		out.flush();
		out.close();
	}
	
	public void clearMsgStatus() throws Exception {
		if (!CommenUtil.isEmpty(uid)) {
			userService.clearUserMsgStatus(uid);
		}
	}

	public Pager<User> getUsers() {
		return users;
	}

	public void setUsers(Pager<User> users) {
		this.users = users;
	}

	public int getcPage() {
		return cPage;
	}

	public void setcPage(int cPage) {
		this.cPage = cPage;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public String getUid() {
		return uid;
	}

	public void setUid(String uid) {
		this.uid = uid;
	}

	public boolean isForbidden() {
		return forbidden;
	}

	public void setForbidden(boolean forbidden) {
		this.forbidden = forbidden;
	}

	public int getStartHour() {
		return startHour;
	}

	public void setStartHour(int startHour) {
		this.startHour = startHour;
	}

	public int getEndHour() {
		return endHour;
	}

	public void setEndHour(int endHour) {
		this.endHour = endHour;
	}

	public int getStartMinute() {
		return startMinute;
	}

	public void setStartMinute(int startMinute) {
		this.startMinute = startMinute;
	}

	public int getEndMinute() {
		return endMinute;
	}

	public void setEndMinute(int endMinute) {
		this.endMinute = endMinute;
	}

	public AdminService getAdminService() {
		return adminService;
	}

	public void setAdminService(AdminService adminService) {
		this.adminService = adminService;
	}

	public String getAdmins() {
		return admins;
	}

	public void setAdmins(String admins) {
		this.admins = admins;
	}

	public String getAid() {
		return aid;
	}

	public void setAid(String aid) {
		this.aid = aid;
	}

}
